//
//  XClockInDetailsViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XClockInDetailsViewController.h"
#import "XClockInEditViewController.h"

#import "FSCalendar.h"

@interface XClockInDetailsViewController ()<FSCalendarDataSource,FSCalendarDelegate>

/// x
@property(nonatomic,strong)UIButton* modify;


@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// --
@property(nonatomic,strong)UILabel* titleLb;

///
@property(nonatomic,strong)UILabel* countLb;


@property(nonatomic,strong)FSCalendar *calendar;

@end

@implementation XClockInDetailsViewController


- (void)loadView{
    [super loadView];
    // 线性布局
    self.view.backgroundColor = COLOR_WHITE;

    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myMargin = 0;
    [self.view  addSubview:contentLayout];
    _contentLayout = contentLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    [self setupViews];
    
    [self updateViews];
}

- (void)setupNavBar{
    
    self.navigationItem.title = self.model.name;

    UIBarButtonItem* modidyItem = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:(UIBarButtonItemStylePlain) target:self action:@selector(modidyClicked:)];
    self.navigationItem.rightBarButtonItem = modidyItem;
    
//    UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [closeBtn setTitle:@"修改" forState:UIControlStateNormal];
//    [closeBtn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
//    [closeBtn addTarget:self action:@selector(modidyClicked:) forControlEvents:UIControlEventTouchUpInside];
//    closeBtn.titleLabel.font = FONT(14);
//    closeBtn.myHeight = PX(44);
//    closeBtn.myWidth = PX(44);
//
//    UIBarButtonItem* modidyItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
//    self.navigationItem.rightBarButtonItem = modidyItem;
}

- (void)setupViews{
    
    MyRelativeLayout* titleLayout = [MyRelativeLayout new];
    titleLayout.backgroundColor = COLOR_BACKGEOUND;
    titleLayout.myHeight = PX(50);
    titleLayout.myMargin = 0;
    titleLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    [self.contentLayout addSubview:titleLayout];
    
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_BASE text:@"当前月份共打卡0次"];
    titleLb.myCenterY = 0;
    titleLb.myHorzMargin = 0;
    [titleLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    
    FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
 
    calendar.appearance.titleFont = FONT_FIT(14);
    calendar.appearance.subtitleFont = FONT_FIT(12);
    calendar.appearance.weekdayFont = FONT_FIT(13);
    calendar.appearance.titleDefaultColor = COLOR_THEME_A;
    calendar.appearance.eventDefaultColor = COLOR_THEME;
    calendar.appearance.eventSelectionColor = COLOR_THEME;
    calendar.appearance.weekdayTextColor = COLOR_GRAY_BASE;
    calendar.appearance.titleDefaultColor = COLOR_BLACK_DARK;
    calendar.appearance.titleSelectionColor = COLOR_WHITE;
    calendar.appearance.titleTodayColor = COLOR_THEME_A;
    calendar.appearance.titleWeekendColor = COLOR_THEME;
    calendar.appearance.todayColor = NULL;
    calendar.appearance.selectionColor = COLOR_ORANGE;
    calendar.appearance.headerDateFormat = @"yyyy年MM月";
    
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文
    calendar.placeholderType = FSCalendarPlaceholderTypeFillHeadTail;
    calendar.firstWeekday = 2;
    calendar.scrollDirection = FSCalendarScrollDirectionHorizontal;
    calendar.dataSource = self;
    calendar.delegate = self;
    calendar.allowsMultipleSelection = YES;
    calendar.swipeToChooseGesture.enabled = YES;
    calendar.backgroundColor = [UIColor whiteColor];
    calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;\
    calendar.today = [NSDate date];
    calendar.myHorzMargin = 16;
    calendar.myTop = 0;
    [self.contentLayout addSubview:calendar];
    self.calendar = calendar;
    

    UILabel* countLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_BASE text:@"未打卡"];
    countLb.textAlignment = NSTextAlignmentCenter;
    countLb.myCenterX = 0;
    countLb.myTop = 20;
    [self.contentLayout addSubview:countLb];
    _countLb = countLb;
}

- (void)updateViews{
    
    self.titleLb.text = [NSString stringWithFormat:@"当前月份共打卡%ld次",self
                         .model.records.count];
    [self.titleLb sizeToFit];
    
    for (int i = 0; i < self.model.records.count; i++) {
        XClockInDate* cdate = self.model.records[i];
        [self.calendar selectDate:cdate.date];
    }
    
    NSDate* xxxDate;
    for (int i = 0; i < self.model.records.count; i++) {
        XClockInDate* cdate = self.model.records[i];
        NSString* dateStr = [cdate.date stringYearMonthDay];
        NSString* nowStr = [[NSDate date] stringYearMonthDay];
        if([dateStr isEqualToString:nowStr]){
            xxxDate = cdate.date;
            break;
        }
    }
    
    if(xxxDate){
        self.countLb.text = [NSString stringWithFormat:@"打开时间:%@",[xxxDate stringYearMonthDayHourMinuteSecond]];
    }else{
        self.countLb.text = [NSString stringWithFormat:@"未打卡:%@",[[NSDate date] stringYearMonthDay]];
    }
    [self.countLb sizeToFit];
}

#pragma mark  --- action
///修改
- (void)modidyClicked:(UIButton*)sender{
    
    XClockInEditViewController* editController = [XClockInEditViewController new];
    editController.operation = EditOperation_Edit;
    editController.model = [self.model mutableCopy];
    editController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        
        self.model = obj;
        
        [self updateViews];
    };
    [UIViewController showViewController:editController animated:YES];
//    [self presentViewController:editController animated:YES completion:NULL];
}

#pragma mark - <FSCalendarDelegateAppearance>

- (NSArray *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventDefaultColorsForDate:(NSDate *)date{
    return @[COLOR_THEME_A];
}


#pragma mark - <FSCalendarDelegate>

//- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
//
//    if(date.month == [NSDate date].month && date.year == [NSDate date].year){
//        return YES;
//    }
//    return NO;
//}

- (BOOL)calendar:(FSCalendar *)calendar shouldDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{

    for (int i = 0; i < self.model.records.count; i++) {
        XClockInDate* cdate = self.model.records[i];
        NSString* dateStr = [cdate.date stringYearMonthDay];
        NSString* nowStr = [date stringYearMonthDay];
        if([dateStr isEqualToString:nowStr]){
            return NO;
            break;
        }
    }
    return YES;
}

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
    
    NSDate* xxxDate;
    for (int i = 0; i < self.model.records.count; i++) {
        XClockInDate* cdate = self.model.records[i];
        NSString* dateStr = [cdate.date stringYearMonthDay];
        NSString* nowStr = [date stringYearMonthDay];
        if([dateStr isEqualToString:nowStr]){
            xxxDate = cdate.date;
            break;
        }
    }
    
    if(xxxDate){
        self.countLb.text = [NSString stringWithFormat:@"打开时间:%@",[xxxDate stringYearMonthDayHourMinuteSecond]];
    }else{
        self.countLb.text = [NSString stringWithFormat:@"未打卡:%@",[date stringYearMonthDay]];
        [self.calendar deselectDate:date];
    }
    [self.countLb sizeToFit];
}

- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
    
}

@end
